package com.xms.schema.service.attribute;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.config.FinalValueDefaults;
import com.xms.core.config.SystemEntityNames;
import com.xms.schema.core.AttributeTypeIds;
import com.xms.schema.core.EntityMask;
import com.xms.schema.core.RelationShipType;
import com.xms.schema.entity.Attribute;
import com.xms.schema.entity.Entity;
import com.xms.schema.entity.RelationShip;
import com.xms.schema.entity.StringMap;
import com.xms.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * DefaultAttributeProvider
 * </p>
 *
 * @author migo
 * @since 2021/9/1
 */
@Service
public class DefaultAttributeProvider implements IDefaultAttributeProvider {
    private IAttributeFinderService _attributeFinderService;

    @Autowired
    public void DefaultAttributeProvider(IAttributeFinderService attributeFinderService) {
        _attributeFinderService = attributeFinderService;
    }

    public DefaultAttributeDatas getSysAttributes(Entity entity)
    {
        DefaultAttributeDatas results = new DefaultAttributeDatas();
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Attribute::getEntityname, SystemEntityNames.SystemUser
                , SystemEntityNames.BusinessUnit
                , SystemEntityNames.Organization
                , SystemEntityNames.WorkFlow);
        queryWrapper.in(Attribute::getName, "systemuserid", "businessunitid", "organizationid", "workflowid");
        List<Attribute> primaryFields = _attributeFinderService.list(queryWrapper);
        Attribute systemUserPrimaryKey = primaryFields.stream().filter(x -> x.getName().equals("systemuserid")).findFirst().get();
        Attribute businessUnitPrimaryKey = primaryFields.stream().filter(x -> x.getName().equals("businessunitid")).findFirst().get();
        Attribute organizationPrimaryKey = primaryFields.stream().filter(x -> x.getName().equals("organizationid")).findFirst().get();
        Attribute workFlowPrimaryKey = primaryFields.stream().filter(x -> x.getName().equals("workflowid")).findFirst().get();
        //插入自动创建的字段
        //主键
        List<Attribute> attrList = new LinkedList<>();
        Attribute primaryAttr = new Attribute();
        primaryAttr.setAttributeid(UUID.randomUUID());
        primaryAttr.setEntityid(entity.getEntityid());
        primaryAttr.setName(entity.getName()+"id");
        primaryAttr.setLocalizedname(entity.getLocalizedname());
        primaryAttr.setAttributetypename(AttributeTypeIds.PRIMARYKEY);
        primaryAttr.setEntityname(entity.getName());
        primaryAttr.setIsnullable(false);
        primaryAttr.setIsrequired(true);
        primaryAttr.setLogenabled(false);
        primaryAttr.setIscustomizable(false);
        primaryAttr.setIscustomfield(false);
        primaryAttr.setAuthorizationenabled(false);
        primaryAttr.setIsprimaryfield(false);
        primaryAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        primaryAttr.setCreatedon(LocalDateTime.now());
        attrList.add(primaryAttr);
        //名称字段
        Attribute nameAttr = new Attribute();
        nameAttr.setAttributeid(UUID.randomUUID());
        nameAttr.setEntityid(entity.getEntityid());
        nameAttr.setName("name");
        nameAttr.setLocalizedname("名称");
        nameAttr.setAttributetypename(AttributeTypeIds.NVARCHAR);
        nameAttr.setEntityname(entity.getName());
        nameAttr.setIsnullable(true);
        nameAttr.setIsrequired(true);
        nameAttr.setLogenabled(false);
        nameAttr.setMaxlength(300);
        nameAttr.setIscustomfield(false);
        nameAttr.setIscustomizable(false);
        nameAttr.setIsprimaryfield(true);
        nameAttr.setAuthorizationenabled(false);
        nameAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        nameAttr.setCreatedon(LocalDateTime.now());
        attrList.add(nameAttr);
        //创建日期字段
        Attribute createdOnAttr = new Attribute();
        createdOnAttr.setAttributeid(UUID.randomUUID());
        createdOnAttr.setEntityid(entity.getEntityid());
        createdOnAttr.setName("createdon");
        createdOnAttr.setLocalizedname("创建日期");
        createdOnAttr.setAttributetypename(AttributeTypeIds.DATETIME);
        createdOnAttr.setEntityname(entity.getName());
        createdOnAttr.setIsnullable(false);
        createdOnAttr.setIsrequired(true);
        createdOnAttr.setLogenabled(false);
        createdOnAttr.setIscustomizable(false);
        createdOnAttr.setIscustomfield(false);
        createdOnAttr.setAuthorizationenabled(false);
        createdOnAttr.setIsprimaryfield(false);
        createdOnAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        createdOnAttr.setCreatedon(LocalDateTime.now());
        attrList.add(createdOnAttr);
        //创建者字段
        Attribute createdByAttr = new Attribute();
        createdByAttr.setAttributeid(UUID.randomUUID());
        createdByAttr.setEntityid(entity.getEntityid());
        createdByAttr.setName("createdby");
        createdByAttr.setLocalizedname("创建者");
        createdByAttr.setAttributetypename(AttributeTypeIds.LOOKUP);
        createdByAttr.setReferencedentityid(systemUserPrimaryKey.getEntityid());
        createdByAttr.setReferencedentityname(SystemEntityNames.SystemUser);
        createdByAttr.setEntityname(entity.getName());
        createdByAttr.setIsnullable(false);
        createdByAttr.setIsrequired(true);
        createdByAttr.setLogenabled(false);
        createdByAttr.setIscustomizable(false);
        createdByAttr.setIscustomfield(false);
        createdByAttr.setAuthorizationenabled(false);
        createdByAttr.setIsprimaryfield(false);
        createdByAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        createdByAttr.setCreatedon(LocalDateTime.now());
        attrList.add(createdByAttr);
        //修改日期字段
        Attribute modifiedOnAttr = new Attribute();
        modifiedOnAttr.setAttributeid(UUID.randomUUID());
        modifiedOnAttr.setEntityid(entity.getEntityid());
        modifiedOnAttr.setName("modifiedon");
        modifiedOnAttr.setLocalizedname("修改日期");
        modifiedOnAttr.setAttributetypename(AttributeTypeIds.DATETIME);
        modifiedOnAttr.setEntityname(entity.getName());
        modifiedOnAttr.setIsnullable(true);
        modifiedOnAttr.setIsrequired(false);
        modifiedOnAttr.setLogenabled(false);
        modifiedOnAttr.setIscustomizable(false);
        modifiedOnAttr.setIscustomfield(false);
        modifiedOnAttr.setAuthorizationenabled(false);
        modifiedOnAttr.setIsprimaryfield(false);
        modifiedOnAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        modifiedOnAttr.setCreatedon(LocalDateTime.now());
        attrList.add(modifiedOnAttr);
        //修改者字段
        Attribute modifiedByAttr = new Attribute();
        modifiedByAttr.setAttributeid(UUID.randomUUID());
        modifiedByAttr.setEntityid(entity.getEntityid());
        modifiedByAttr.setName("modifiedby");
        modifiedByAttr.setLocalizedname("修改者");
        modifiedByAttr.setAttributetypename(AttributeTypeIds.LOOKUP);
        modifiedByAttr.setReferencedentityid(systemUserPrimaryKey.getEntityid());
        modifiedByAttr.setReferencedentityname(SystemEntityNames.SystemUser);
        modifiedByAttr.setEntityname(entity.getName());
        modifiedByAttr.setIsnullable(true);
        modifiedByAttr.setIsrequired(false);
        modifiedByAttr.setLogenabled(false);
        modifiedByAttr.setIscustomizable(false);
        modifiedByAttr.setIscustomfield(false);
        modifiedByAttr.setAuthorizationenabled(false);
        modifiedByAttr.setIsprimaryfield(false);
        modifiedByAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        modifiedByAttr.setCreatedon(LocalDateTime.now());
        attrList.add(modifiedByAttr);
        //状态字段
        Attribute stateCodeAttr = new Attribute();
        stateCodeAttr.setAttributeid(UUID.randomUUID());
        stateCodeAttr.setEntityid(entity.getEntityid());
        stateCodeAttr.setName("statecode");
        stateCodeAttr.setLocalizedname("状态");
        stateCodeAttr.setAttributetypename(AttributeTypeIds.STATE);
        stateCodeAttr.setEntityname(entity.getName());
        stateCodeAttr.setIsnullable(false);
        stateCodeAttr.setIsrequired(true);
        stateCodeAttr.setLogenabled(false);
        stateCodeAttr.setIscustomizable(false);
        stateCodeAttr.setIscustomfield(false);
        stateCodeAttr.setAuthorizationenabled(false);
        stateCodeAttr.setIsprimaryfield(false);
        stateCodeAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
        stateCodeAttr.setCreatedon(LocalDateTime.now());
        attrList.add(stateCodeAttr);
        //字段选项
        List<StringMap> statePicklist = new LinkedList<>();
        StringMap stateCodeMap = new StringMap();
        stateCodeMap.setStringmapid(UUID.randomUUID());
        stateCodeMap.setAttributeid(stateCodeAttr.getAttributeid());
        stateCodeMap.setName("启用");
        stateCodeMap.setValue(1);
        stateCodeMap.setDisplayorder(0);
        stateCodeMap.setAttributename("statecode");
        stateCodeMap.setEntityname(entity.getName());
        statePicklist.add(stateCodeMap);
        StringMap stateCodeMap2 = new StringMap();
        stateCodeMap2.setStringmapid(UUID.randomUUID());
        stateCodeMap2.setAttributeid(stateCodeAttr.getAttributeid());
        stateCodeMap2.setName("禁用");
        stateCodeMap2.setValue(0);
        stateCodeMap2.setDisplayorder(0);
        stateCodeMap2.setAttributename("statecode");
        stateCodeMap2.setEntityname(entity.getName());
        statePicklist.add(stateCodeMap2);
        //stateCodeAttr.setPickLists(statePicklist);
        results.picklists = statePicklist;
        //状态描述字段
        /*Attribute statusCodeAttr = new Attribute();
        statusCodeAttr.setEntityid(entity.getEntityid());
        statusCodeAttr.setName("statuscode");
        statusCodeAttr.setLocalizedname("状态描述");
        statusCodeAttr.setAttributetypename(AttributeTypeIds.STATUS);
        statusCodeAttr.setEntityname(entity.getName());
        statusCodeAttr.setIsnullable(false);
        statusCodeAttr.setIsrequired(true);
        statusCodeAttr.setLogenabled(false);
        statusCodeAttr.setIscustomizable(false);
        statusCodeAttr.setIscustomfield(false);
        statusCodeAttr.setDefaultvalue("0");
        statusCodeAttr.setCreatedby(UUID.fromString("611b70c7-9584-46e6-90d4-63c269aa133c"));
        //选项集
        Optionset os = new Optionset();
        os.setCreatedby(UUID.fromString("611b70c7-9584-46e6-90d4-63c269aa133c"));
        os.setName(statusCodeAttr.getLocalizedname());
        os.setIspublic(false);
        os.setOptionsetid(UUID.randomUUID());
        os.setOrganizationid(UUID.fromString("00000000-0000-0000-0000-000000000001"));
        Optionsetdetail osDetail1 = new Optionsetdetail();
        osDetail1.setOptionsetdetailid(UUID.randomUUID());
        osDetail1.setOptionsetid(os.getOptionsetid());
        osDetail1.setDisplayorder(0);
        osDetail1.setIsselected(false);
        osDetail1.setName("草稿");
        osDetail1.setValue(0);
        Optionsetdetail osDetail2 = new Optionsetdetail();
        osDetail2.setOptionsetdetailid(UUID.randomUUID());
        osDetail2.setOptionsetid(os.getOptionsetid());
        osDetail2.setDisplayorder(1);
        osDetail2.setIsselected(false);
        osDetail2.setName("激活");
        osDetail2.setValue(1);
        Optionsetdetail osDetail3 = new Optionsetdetail();
        osDetail3.setOptionsetdetailid(UUID.randomUUID());
        osDetail3.setOptionsetid(os.getOptionsetid());
        osDetail3.setDisplayorder(1);
        osDetail3.setIsselected(false);
        osDetail3.setName("禁用");
        osDetail3.setValue(2);
        os.Items = new List<Domain.OptionSetDetail>() { osDetail1, osDetail2, osDetail3 };
        statusCodeAttr.OptionSet = os;
        statusCodeAttr.OptionSetId = os.OptionSetId;
        attrList.add(statusCodeAttr);*/

        //实体范围
        if (entity.getEntitymask() == EntityMask.User.value()) {
            Attribute ownerIdAttr = new Attribute();
            ownerIdAttr.setAttributeid(UUID.randomUUID());
            ownerIdAttr.setEntityid(entity.getEntityid());
            ownerIdAttr.setName("ownerid");
            ownerIdAttr.setLocalizedname("所有者");
            ownerIdAttr.setAttributetypename(AttributeTypeIds.OWNER);
            ownerIdAttr.setReferencedentityid(systemUserPrimaryKey.getEntityid());
            ownerIdAttr.setReferencedentityname(SystemEntityNames.SystemUser);
            ownerIdAttr.setEntityname(entity.getName());
            ownerIdAttr.setIsnullable(false);
            ownerIdAttr.setIsrequired(true);
            ownerIdAttr.setLogenabled(false);
            ownerIdAttr.setIscustomizable(false);
            ownerIdAttr.setIscustomfield(false);
            ownerIdAttr.setAuthorizationenabled(false);
            ownerIdAttr.setIsprimaryfield(false);
            ownerIdAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
            ownerIdAttr.setCreatedon(LocalDateTime.now());
            attrList.add(ownerIdAttr);
            Attribute ownerIdTypeAttr = new Attribute();
            ownerIdTypeAttr.setAttributeid(UUID.randomUUID());
            ownerIdTypeAttr.setEntityid(entity.getEntityid());
            ownerIdTypeAttr.setName("owneridtype");
            ownerIdTypeAttr.setLocalizedname("所有者类型");
            ownerIdTypeAttr.setAttributetypename(AttributeTypeIds.INT);
            ownerIdTypeAttr.setEntityname(entity.getName());
            ownerIdTypeAttr.setIsnullable(false);
            ownerIdTypeAttr.setIsrequired(true);
            ownerIdTypeAttr.setLogenabled(false);
            ownerIdTypeAttr.setIscustomizable(false);
            ownerIdTypeAttr.setIscustomfield(false);
            ownerIdTypeAttr.setAuthorizationenabled(false);
            ownerIdTypeAttr.setIsprimaryfield(false);
            ownerIdTypeAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
            ownerIdTypeAttr.setCreatedon(LocalDateTime.now());
            attrList.add(ownerIdTypeAttr);
            Attribute owningBusinessUnitAttr = new Attribute();
            owningBusinessUnitAttr.setAttributeid(UUID.randomUUID());
            owningBusinessUnitAttr.setEntityid(entity.getEntityid());
            owningBusinessUnitAttr.setName("owningbusinessunit");
            owningBusinessUnitAttr.setLocalizedname("所有者部门");
            owningBusinessUnitAttr.setAttributetypename(AttributeTypeIds.LOOKUP);
            owningBusinessUnitAttr.setReferencedentityid(businessUnitPrimaryKey.getEntityid());
            owningBusinessUnitAttr.setReferencedentityname(SystemEntityNames.BusinessUnit);
            owningBusinessUnitAttr.setEntityname(entity.getName());
            owningBusinessUnitAttr.setIsnullable(true);
            owningBusinessUnitAttr.setIsrequired(true);
            owningBusinessUnitAttr.setLogenabled(false);
            owningBusinessUnitAttr.setIscustomizable(false);
            owningBusinessUnitAttr.setIscustomfield(false);
            owningBusinessUnitAttr.setAuthorizationenabled(false);
            owningBusinessUnitAttr.setIsprimaryfield(false);
            owningBusinessUnitAttr.setCreatedon(LocalDateTime.now());
            owningBusinessUnitAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
            attrList.add(owningBusinessUnitAttr);
            Attribute organizationIdAttr = new Attribute();
            organizationIdAttr.setAttributeid(UUID.randomUUID());
            organizationIdAttr.setEntityid(entity.getEntityid());
            organizationIdAttr.setName("organizationid");
            organizationIdAttr.setLocalizedname("组织");
            organizationIdAttr.setAttributetypename(AttributeTypeIds.LOOKUP);
            organizationIdAttr.setReferencedentityid(organizationPrimaryKey.getEntityid());
            organizationIdAttr.setReferencedentityname(SystemEntityNames.Organization);
            organizationIdAttr.setEntityname(entity.getName());
            organizationIdAttr.setIsnullable(false);
            organizationIdAttr.setIsrequired(true);
            organizationIdAttr.setLogenabled(false);
            organizationIdAttr.setIscustomizable(false);
            organizationIdAttr.setIscustomfield(false);
            organizationIdAttr.setAuthorizationenabled(false);
            organizationIdAttr.setIsprimaryfield(false);
            organizationIdAttr.setCreatedon(LocalDateTime.now());
            organizationIdAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
            attrList.add(organizationIdAttr);
        }
        //启用审批流
        if (entity.getWorkflowenabled())
        {
            Attribute workflowIdAttr = new Attribute();
            workflowIdAttr.setAttributeid(UUID.randomUUID());
            workflowIdAttr.setEntityid(entity.getEntityid());
            workflowIdAttr.setName("workflowid");
            workflowIdAttr.setLocalizedname("审批流");
            workflowIdAttr.setAttributetypename(AttributeTypeIds.LOOKUP);
            workflowIdAttr.setReferencedentityid(workFlowPrimaryKey.getEntityid());
            workflowIdAttr.setReferencedentityname(SystemEntityNames.WorkFlow);
            workflowIdAttr.setEntityname(entity.getName());
            workflowIdAttr.setIsnullable(true);
            workflowIdAttr.setIsrequired(false);
            workflowIdAttr.setLogenabled(false);
            workflowIdAttr.setIscustomizable(false);
            workflowIdAttr.setIscustomfield(false);
            workflowIdAttr.setAuthorizationenabled(false);
            workflowIdAttr.setIsprimaryfield(false);
            workflowIdAttr.setCreatedon(LocalDateTime.now());
            workflowIdAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
            attrList.add(workflowIdAttr);
            Attribute processStateAttr = new Attribute();
            processStateAttr.setAttributeid(UUID.randomUUID());
            processStateAttr.setEntityid(entity.getEntityid());
            processStateAttr.setName("processstate");
            processStateAttr.setLocalizedname("审批状态");
            processStateAttr.setAttributetypename(AttributeTypeIds.PICKLIST);
            processStateAttr.setEntityname(entity.getName());
            processStateAttr.setIsnullable(true);
            processStateAttr.setIsrequired(false);
            processStateAttr.setLogenabled(false);
            processStateAttr.setIscustomizable(false);
            processStateAttr.setIscustomfield(false);
            processStateAttr.setAuthorizationenabled(false);
            processStateAttr.setIsprimaryfield(false);
            processStateAttr.setDefaultvalue("-1");
            processStateAttr.setOptionsetid(UUIDUtil.get("00000000-0000-0000-0000-000000000001"));
            processStateAttr.setCreatedon(LocalDateTime.now());
            processStateAttr.setCreatedby(FinalValueDefaults.ADMIN_USERID);
            attrList.add(processStateAttr);
        }
        results.attributes = attrList;
        results.relationships = this.getSysAttributeRelationShips(entity, attrList);
        return results;
    }

    public List<RelationShip> getSysAttributeRelationShips(Entity entity, List<Attribute> sysAttributes)
    {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Attribute::getEntityname, "systemuser", "businessunit", "organization", "workflow");
        queryWrapper.in(Attribute::getName, "systemuserid", "businessunitid", "organizationid", "workflowid");
        List<Attribute> primaryFields = _attributeFinderService.list(queryWrapper);
        Attribute systemUserPrimaryKey = primaryFields.stream().filter(x -> x.getName().equalsIgnoreCase("systemuserid")).findFirst().get();
        Attribute businessUnitPrimaryKey = primaryFields.stream().filter(x -> x.getName().equalsIgnoreCase("businessunitid")).findFirst().get();
        Attribute organizationPrimaryKey = primaryFields.stream().filter(x -> x.getName().equalsIgnoreCase("organizationid")).findFirst().get();
        Attribute workFlowPrimaryKey = primaryFields.stream().filter(x -> x.getName().equalsIgnoreCase("workflowid")).findFirst().get();
        List<RelationShip> relationships = new ArrayList<>();
        if (sysAttributes.stream().anyMatch(n -> n.getName().equalsIgnoreCase("createdby")))
        {
            RelationShip createdByRelationShip = new RelationShip();
            createdByRelationShip.setRelationshipid(UUID.randomUUID());
            createdByRelationShip.setName("lk_" + entity.getName() + "_createdby");
            createdByRelationShip.setRelationshiptype(RelationShipType.ManyToOne.value());
            createdByRelationShip.setReferencingattributeid(sysAttributes.stream().filter(n -> n.getName().equalsIgnoreCase("createdby")).findFirst().get().getAttributeid());
            createdByRelationShip.setReferencingattributename("createdby");
            createdByRelationShip.setReferencingentityid(entity.getEntityid());
            createdByRelationShip.setReferencingentityname(entity.getName());
            createdByRelationShip.setReferencedattributeid(systemUserPrimaryKey.getAttributeid());
            createdByRelationShip.setReferencedattributename("systemuserid");
            createdByRelationShip.setReferencedentityid(systemUserPrimaryKey.getEntityid());
            createdByRelationShip.setReferencedentityname("systemuser");
            createdByRelationShip.setIscustomizable(false);
            createdByRelationShip.setCascadelinkmask(0);
            createdByRelationShip.setCascadeassign(0);
            createdByRelationShip.setCascadedelete(0);
            createdByRelationShip.setCascadeshare(0);
            createdByRelationShip.setCascadeunshare(0);
            relationships.add(createdByRelationShip);
        }
        if (sysAttributes.stream().anyMatch(n -> n.getName().equalsIgnoreCase("modifiedby")))
        {
            RelationShip modifiedByRelationShip = new RelationShip();
            modifiedByRelationShip.setRelationshipid(UUID.randomUUID());
            modifiedByRelationShip.setName("lk_" + entity.getName() + "_modifiedby");
            modifiedByRelationShip.setRelationshiptype(RelationShipType.ManyToOne.value());
            modifiedByRelationShip.setReferencingattributeid(sysAttributes.stream().filter(n -> n.getName().equalsIgnoreCase("modifiedby")).findFirst().get().getAttributeid());
            modifiedByRelationShip.setReferencingattributename("modifiedby");
            modifiedByRelationShip.setReferencingentityid(entity.getEntityid());
            modifiedByRelationShip.setReferencingentityname(entity.getName());
            modifiedByRelationShip.setReferencedattributeid(systemUserPrimaryKey.getAttributeid());
            modifiedByRelationShip.setReferencedattributename("systemuserid");
            modifiedByRelationShip.setReferencedentityid(systemUserPrimaryKey.getEntityid());
            modifiedByRelationShip.setReferencedentityname("systemuser");
            modifiedByRelationShip.setIscustomizable(false);
            modifiedByRelationShip.setCascadelinkmask(0);
            modifiedByRelationShip.setCascadeassign(0);
            modifiedByRelationShip.setCascadedelete(0);
            modifiedByRelationShip.setCascadeshare(0);
            modifiedByRelationShip.setCascadeunshare(0);
            relationships.add(modifiedByRelationShip);
        }
        if (entity.getEntitymask() == EntityMask.User.value())
        {
            if (sysAttributes.stream().anyMatch(n -> n.getName().equalsIgnoreCase("ownerid")))
            {
                RelationShip ownerIdRelationShip = new RelationShip();
                ownerIdRelationShip.setRelationshipid(UUID.randomUUID());
                ownerIdRelationShip.setName("lk_" + entity.getName() + "_ownerid");
                ownerIdRelationShip.setRelationshiptype(RelationShipType.ManyToOne.value());
                ownerIdRelationShip.setReferencingattributeid(sysAttributes.stream().filter(n -> n.getName().equalsIgnoreCase("ownerid")).findFirst().get().getAttributeid());
                ownerIdRelationShip.setReferencingattributename("ownerid");
                ownerIdRelationShip.setReferencingentityid(entity.getEntityid());
                ownerIdRelationShip.setReferencingentityname(entity.getName());
                ownerIdRelationShip.setReferencedattributeid(systemUserPrimaryKey.getAttributeid());
                ownerIdRelationShip.setReferencedattributename("systemuserid");
                ownerIdRelationShip.setReferencedentityid(systemUserPrimaryKey.getEntityid());
                ownerIdRelationShip.setReferencedentityname("systemuser");
                ownerIdRelationShip.setIscustomizable(false);
                ownerIdRelationShip.setCascadelinkmask(0);
                ownerIdRelationShip.setCascadeassign(0);
                ownerIdRelationShip.setCascadedelete(0);
                ownerIdRelationShip.setCascadeshare(0);
                ownerIdRelationShip.setCascadeunshare(0);
                relationships.add(ownerIdRelationShip);
            }
            if (sysAttributes.stream().anyMatch(n -> n.getName().equalsIgnoreCase("owningbusinessunit")))
            {
                RelationShip owningBusinessUnitRelationShip = new RelationShip();
                owningBusinessUnitRelationShip.setRelationshipid(UUID.randomUUID());
                owningBusinessUnitRelationShip.setName("lk_" + entity.getName() + "_owningbusinessunit");
                owningBusinessUnitRelationShip.setRelationshiptype(RelationShipType.ManyToOne.value());
                owningBusinessUnitRelationShip.setReferencingattributeid(sysAttributes.stream().filter(n -> n.getName().equalsIgnoreCase("owningbusinessunit")).findFirst().get().getAttributeid());
                owningBusinessUnitRelationShip.setReferencingattributename("owningbusinessunit");
                owningBusinessUnitRelationShip.setReferencingentityid(entity.getEntityid());
                owningBusinessUnitRelationShip.setReferencingentityname(entity.getName());
                owningBusinessUnitRelationShip.setReferencedattributeid(businessUnitPrimaryKey.getAttributeid());
                owningBusinessUnitRelationShip.setReferencedattributename("businessunitid");
                owningBusinessUnitRelationShip.setReferencedentityid(businessUnitPrimaryKey.getEntityid());
                owningBusinessUnitRelationShip.setReferencedentityname("businessunit");
                owningBusinessUnitRelationShip.setIscustomizable(false);
                owningBusinessUnitRelationShip.setCascadelinkmask(0);
                owningBusinessUnitRelationShip.setCascadeassign(0);
                owningBusinessUnitRelationShip.setCascadedelete(0);
                owningBusinessUnitRelationShip.setCascadeshare(0);
                owningBusinessUnitRelationShip.setCascadeunshare(0);
                relationships.add(owningBusinessUnitRelationShip);
            }
        }
        if (sysAttributes.stream().anyMatch(n -> n.getName().equals("organizationid")))
        {
            RelationShip organizationIdRelationShip = new RelationShip();
            organizationIdRelationShip.setRelationshipid(UUID.randomUUID());
            organizationIdRelationShip.setName("lk_" + entity.getName() + "_organizationid");
            organizationIdRelationShip.setRelationshiptype(RelationShipType.ManyToOne.value());
            organizationIdRelationShip.setReferencingattributeid(sysAttributes.stream().filter(n -> n.getName().equalsIgnoreCase("organizationid")).findFirst().get().getAttributeid());
            organizationIdRelationShip.setReferencingattributename("organizationid");
            organizationIdRelationShip.setReferencingentityid(entity.getEntityid());
            organizationIdRelationShip.setReferencingentityname(entity.getName());
            organizationIdRelationShip.setReferencedattributeid(organizationPrimaryKey.getAttributeid());
            organizationIdRelationShip.setReferencedattributename("organizationid");
            organizationIdRelationShip.setReferencedentityid(organizationPrimaryKey.getEntityid());
            organizationIdRelationShip.setReferencedentityname("organization");
            organizationIdRelationShip.setIscustomizable(false);
            organizationIdRelationShip.setCascadelinkmask(0);
            organizationIdRelationShip.setCascadeassign(0);
            organizationIdRelationShip.setCascadedelete(0);
            organizationIdRelationShip.setCascadeshare(0);
            organizationIdRelationShip.setCascadeunshare(0);
            relationships.add(organizationIdRelationShip);
        }
        if (entity.getWorkflowenabled())
        {
            if (sysAttributes.stream().anyMatch(n -> n.getName().equalsIgnoreCase("workflowid")))
            {
                RelationShip workflowIdRelationShip = new RelationShip();
                workflowIdRelationShip.setRelationshipid(UUID.randomUUID());
                workflowIdRelationShip.setName("lk_" + entity.getName() + "_workflowid");
                workflowIdRelationShip.setRelationshiptype(RelationShipType.ManyToOne.value());
                workflowIdRelationShip.setReferencingattributeid(sysAttributes.stream().filter(n -> n.getName().equalsIgnoreCase("workflowid")).findFirst().get().getAttributeid());
                workflowIdRelationShip.setReferencingattributename("workflowid");
                workflowIdRelationShip.setReferencingentityid(entity.getEntityid());
                workflowIdRelationShip.setReferencingentityname(entity.getName());
                workflowIdRelationShip.setReferencedattributeid(workFlowPrimaryKey.getAttributeid());
                workflowIdRelationShip.setReferencedattributename("workflowid");
                workflowIdRelationShip.setReferencedentityid(workFlowPrimaryKey.getEntityid());
                workflowIdRelationShip.setReferencedentityname("workflow");
                workflowIdRelationShip.setIscustomizable(false);
                workflowIdRelationShip.setCascadelinkmask(0);
                workflowIdRelationShip.setCascadeassign(0);
                workflowIdRelationShip.setCascadedelete(0);
                workflowIdRelationShip.setCascadeshare(0);
                workflowIdRelationShip.setCascadeunshare(0);
                relationships.add(workflowIdRelationShip);
            }
        }
        return relationships;
    }
}

